// @ts-nocheck
/**
 * @Date: 2022-04-17 19:25:54
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-07-15 20:51:35
 * @FilePath: \weibo-api\controller\atCtl.js
 * @Description: at数据控制
 */
const { AtRelation, Blog, User, Reply, AtBlog } = require("../model/index");
const { SuccessModel, ErrorModel } = require("../ResModel");
//获取@我的数量
const getMeCount = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  const AtWhereOpt = { userId, isRead: false };
  let BlogRes = await AtBlog.findAndCountAll({
    where: AtWhereOpt,
  });
  let AtRes = await AtRelation.findAndCountAll({
    where: AtWhereOpt,
  });
  ctx.body = new SuccessModel({
    count: parseInt(BlogRes.count) + parseInt(AtRes.count),
  });
};
//获取@我的的博客列表
const getAtMeBlogList = async (ctx, next) => {
  let { currentPage = 1, pageSize = 5 } = ctx.query;
  const { id: userId } = ctx.session.userInfo;
  currentPage = parseInt(currentPage);
  pageSize = parseInt(pageSize);
  const res = await Blog.findAndCountAll({
    limit: pageSize, //每页条数
    offset: (currentPage - 1) * pageSize, //跳过条数
    order: [["id", "desc"]],
    include: [
      {
        model: AtBlog,
        attributes: ["userId", "blogId"],
        where: { userId, isRead: false },
      },
      {
        model: User,
        attributes: ["id", "userName", "nickName", "avatar"],
      },
    ],
  });
  let blogList = res.rows.map((row) => row.dataValues);
  blogList = blogList.map((blogItem) => {
    const user = blogItem.user.dataValues;
    blogItem.user = user;
    return blogItem;
  });
  // console.log("!@#!#!#", blogList);
  ctx.body = new SuccessModel({
    count: res.count,
    blogList,
  });
};
//获取@我的评论列表
const getAtMeReplyList = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  let { currentPage = 1, pageSize = 5 } = ctx.query;
  currentPage = parseInt(currentPage);
  pageSize = parseInt(pageSize);
  let res = await Reply.findAndCountAll({
    limit: pageSize, //每页条数
    offset: (currentPage - 1) * pageSize, //跳过条数
    order: [["id", "desc"]],
    where: { reply_userId: userId },
    attributes: [
      "id",
      "commentId",
      "parantId",
      "userId",
      "reply_userId",
      "content",
      "blogId",
      "createdAt",
    ],
    include: [
      {
        model: AtRelation,
        // attributes: ["userId", "ReplyId"],
        where: { isRead: false },
      },
      {
        model: User,
        attributes: ["id", "userName", "nickName", "avatar"],
      },
    ],
  });
  let replyList = res.rows.map((row) => row.dataValues);
  // console.log("查询结果是", res, replyList);
  ctx.body = new SuccessModel({
    count: res.count,
    replyList,
  });
};
//标记为已读（回复）  可以已读评论回复 或者已读回复的回复 不传默认全部已读
const markAsRead = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  const { blogId, ReplyId, commentId } = ctx.params;
  const newData = { isRead: true };
  const whereOpt = {};
  if (blogId && blogId != -1) {
    whereOpt.blogId = parseInt(blogId);
  } else if (ReplyId && ReplyId != -1) {
    whereOpt.ReplyId = parseInt(ReplyId);
  } else if (commentId && commentId != -1) {
    whereOpt.commentId = parseInt(commentId);
  }
  whereOpt.userId = userId;
  try {
    let res = await AtRelation.update(newData, {
      where: whereOpt,
    });
    if (res[0] > 0) {
      ctx.body = new SuccessModel();
    }
  } catch (error) {
    console.error(error);
    ctx.throw("更新失败", error);
  }
};
//标记为已读（博客提到我的）
const markBlogAsRead = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  const { blogId } = ctx.params;
  const newData = { isRead: true };
  const WhereOpt = {};
  if (blogId != -1) {
    WhereOpt.blogId = blogId;
  }
  WhereOpt.userId = userId;
  try {
    let res = await AtBlog.update(newData, {
      where: WhereOpt,
    });
    if (res[0] > 0) {
      ctx.body = new SuccessModel();
    }
  } catch (error) {
    console.error(error);
    ctx.throw("更新失败", error);
  }
};
module.exports = {
  getMeCount,
  getAtMeBlogList,
  getAtMeReplyList,
  markAsRead,
  markBlogAsRead,
};
